package com.feilong.edgeAI.speech;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import androidx.appcompat.app.AlertDialog;

import com.blankj.utilcode.util.ToastUtils;
import com.feilong.edgeAI.R;
import com.feilong.edgeAI.model.NetWorkError;
import com.iflytek.aikit.core.AiHelper;
import com.iflytek.aikit.core.AuthListener;
import com.iflytek.aikit.core.BaseLibrary;
import com.iflytek.aikit.core.CoreListener;
import com.iflytek.aikit.core.ErrType;
import com.iflytek.aikit.core.JLibrary;
import com.xuexiang.xui.widget.dialog.DialogLoader;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
//import com.iflytek.aikit.core.ErrType;

/**
 * Created by【仿脑科技】
 *
 * @Author: potato
 * @CreateDate: 2023/11/14 11:24
 */
public class AbilityInit {
    private static final String TAG = "feilongedgeAI";
    public static final String IFLYTEK_DIR = ".iflytek";//讯飞离线资料在根目录的.iflytek文件夹
    public static final String IFLYTEK_ROOT = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + IFLYTEK_DIR;
    public static boolean IFLYTECK_FAIL_BY_NO_NETWORK;
    public Context context;
    /**
     * 初始化能力值
     * @param context
     */
    public void init(Context context) {
        this.context = context;
        BaseLibrary.Params params = BaseLibrary.Params.builder()
                .appId(context.getResources().getString(R.string.appId))
                .apiKey(context.getResources().getString(R.string.apiKey))
                .apiSecret(context.getResources().getString(R.string.apiSecret))
                .workDir(IFLYTEK_ROOT)
                .iLogMaxCount(1)
                .authInterval(300)
                .ability(
                        "e867a88f2"
                                + ";e2e44feff"
                                + ";ee62fa27c"
                                + ";ed4f63e83"
                )
                .build();
       /* AiHelper.getInst().registerListener(new AuthListener() {
            @SuppressLint("LongLogTag")
            @Override
            public void onAuthStateChange(ErrType errType, int code) {
                switch (errType) {
                    case AUTH:
                        Log.i(TAG, "huanxing SDK状态：授权结果码" + code);
                        break;
                    case HTTP:
                        Log.i(TAG, "SDK状态：HTTP认证结果" + code);
                        break;
                    default:
                        Log.i(TAG, "SDK状态：其他错误");
                }
            }
        });*/
        AiHelper.getInst().registerListener(new CoreListener() {
            @SuppressLint("LongLogTag")
            @Override
            public void onAuthStateChange(ErrType errType, int code) {
                Log.i(TAG, "AbilityInit onAuthStateChange code:" + code);
                switch (code) {
                    case 0:
                        if (null != onAuthSuccessListener) {
                            onAuthSuccessListener.onAuthSuccess();
                        }
                        Log.d(TAG, "SDK状态：授权结果码" + code);
                        break;
                    case 18701:
                        ToastUtils.showLong(R.string.network_error);
                        EventBus.getDefault().post(new NetWorkError());
                        Log.i(TAG, "请先连接网络");
                        IFLYTECK_FAIL_BY_NO_NETWORK = true;
                        break;
                    default:
                        Log.d(TAG, "SDK状态：其他错误");
                        break;
                }
                Log.i(TAG, "设备指纹:" + JLibrary.getInst().getDeviceId());
            }
        });
        AiHelper.getInst().init(context, params);
    }

    private OnAuthSuccessListener onAuthSuccessListener;

    public void setOnAuthSuccessListener(OnAuthSuccessListener onAuthSuccessListener) {
        this.onAuthSuccessListener = onAuthSuccessListener;
    }

    public interface OnAuthSuccessListener {
        void onAuthSuccess();

        void onAuthFailure();
    }
}
